package com.chat.security.service;

import com.chat.security.entity.SecurityUser;
import com.chat.system.entity.po.ChatUser;
import com.chat.system.service.IChatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class UserService implements UserDetailsService {

    @Autowired
    IChatUserService chatUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        final ChatUser chatUser = chatUserService.getById(username);
        if (chatUser == null){
            throw  new UsernameNotFoundException("not found");
        }
        //TODO 差数据库。
        SecurityUser securityUser = new SecurityUser();
        // root
        securityUser.setChatUser(new ChatUser(chatUser.getUserName(),chatUser.getPasswd()));
        return securityUser;
    }
}
